package henu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import henu.IF.ManagementClassIF;
import henu.IF.ManagementMessageIF;
import henu.IF.ManagementTeacherIF;
import henu.bean.Classbean;
import henu.bean.Message;
import henu.bean.Teacher;
import henu.factory.DaoFactory;

@WebServlet("/ManagementMessage")
public class ManagementMessage extends HttpServlet{
	
	
	
	public ManagementMessage() {
        super();
        // TODO Auto-generated constructor stub
    }
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		myway(request,response);
	}
	
	
	private void myway(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String method = request.getParameter("method");
		switch(method){
		      case "addMessage"   		: addMessage(request, response);    break;
		      case "findSendMessage"  	: findSendMessage(request, response); break;
		      case "findAcceptMessage"  : findAcceptMessage(request, response); break;
		      case "deleteMessage"   	: deleteMessage(request, response);    break;
		      case "replyMessage"       : replyMessage(request, response);    break;
		    
		}
		
	}
	
	
	private void addMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String sid = request.getSession().getAttribute("sid").toString();
		ManagementMessageIF mm=DaoFactory.getManagementMessageDaoInstance();
		Message message =new Message();
		message.setSid(sid);
		message.setTid(request.getParameter("tid"));
		message.setContent(request.getParameter("content"));
		boolean result=mm.addMessage(message);
		PrintWriter   out   =   response.getWriter(); 
		if(result)
		{
            out.println("<script>alert('留言成功!');</script>");
            out.println("<script>window.location.href='/MyexamOnline/student/addMessage.jsp'</script>");
        }
		else 
		{
            out.println("<script>alert('添加失败!');history.back();</script>"); 
		}
	}
	
	
	private void findSendMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ManagementMessageIF mm = DaoFactory.getManagementMessageDaoInstance();
		
		
		String sid = request.getSession().getAttribute("sid").toString();
		List<Message> list = mm.findSendMessage(sid);
		request.getSession().setAttribute("messageList", list);
		response.sendRedirect("student/findSendMessage.jsp"); 
	}
	
	private void findAcceptMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ManagementMessageIF mm = DaoFactory.getManagementMessageDaoInstance();
		
		
		String tid = request.getSession().getAttribute("tid").toString();
		List<Message> list = mm.findAcceptMessage(tid);
		request.getSession().setAttribute("messageList", list);
		response.sendRedirect("teacher/findAcceptMessage.jsp");
	}
	
	
	private void deleteMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ManagementMessageIF mm=DaoFactory.getManagementMessageDaoInstance();
		String sid = request.getSession().getAttribute("sid").toString();
		int mid = Integer.parseInt(request.getParameter("mid"));
		boolean result = mm.deleteMessage(mid);
		PrintWriter   out   =   response.getWriter(); 
		if(result){
		     List<Message> list = mm.findSendMessage(sid);
		     request.getSession().setAttribute("messageList", list);
		     out.println("<script>alert('删除成功!');</script>");
             out.println("<script>window.location.href='/MyexamOnline/student/findSendMessage.jsp'</script>");
		}
		else
		{
			out.println("<script>alert('删除失败!');history.back();</script>");
		}
	}
	
	
	private void replyMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String tid = request.getSession().getAttribute("tid").toString();
		
		ManagementMessageIF mm=DaoFactory.getManagementMessageDaoInstance();
		int mid= Integer.parseInt(request.getParameter("mid"));
		String reply=request.getParameter("reply");
		
		Message message = new Message();
		message.setMid(mid);
		message.setReply(reply);
	
		boolean result=mm.updateMessage(message);
		PrintWriter   out   = response.getWriter(); 
		if(result){
		     List<Message> list = mm.findAcceptMessage(tid);
		     request.getSession().setAttribute("messageList", list);
		     out.println("<script>alert('回复成功!');</script>");
             out.println("<script>window.location.href='/MyexamOnline/teacher/findAcceptMessage.jsp'</script>");
		}
		else
		{
			out.println("<script>alert('修改失败!');history.back();</script>");
		}
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	

}
